# coding: utf-8
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QMouseEvent, QColor
from PySide6.QtWidgets import QVBoxLayout, QSizePolicy, QHBoxLayout, QFrame
from qfluentwidgets import ProgressBar, BodyLabel, setFont, CardWidget

from common import SingleIcon, StyleSheet
from ..widgets import ImageLabel, ElidedBodyLabel, TagLabel


class BookCard(QFrame):
    clacked = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.coverLabel = ImageLabel(self)
        self.titleLabel = ElidedBodyLabel(self)
        self.archivedFinalScoreLabel = BodyLabel(self)
        self.horizontalLayout = QHBoxLayout()
        self.progressBar = ProgressBar(self)
        self.scoreLabel = BodyLabel(self)

        self.horizontalLayout_2 = QHBoxLayout(self.coverLabel)
        self.statusLabel = TagLabel(self)
        self.courseClassCreditLabel = TagLabel(self)

        self.__initWidgets()
        self.__initLayout()

    def __initWidgets(self):
        self.coverLabel.setMinimumSize(158, 227)
        self.setMinimumSize(200, 310)
        self.coverLabel.setRadius(5)

        setFont(self.titleLabel, 16)
        setFont(self.archivedFinalScoreLabel, 12)
        setFont(self.scoreLabel, 12)
        self.archivedFinalScoreLabel.setTextColor(QColor(0, 0, 0, 150), QColor(255, 255, 255, 150))

        StyleSheet.CARD.apply(self)

    def __initLayout(self):
        self.archivedFinalScoreLabel.setSizePolicy(
            QSizePolicy.Policy.Expanding,
            QSizePolicy.Policy.Fixed
        )
        self.progressBar.setSizePolicy(
            QSizePolicy.Policy.Expanding,
            QSizePolicy.Policy.Fixed
        )
        self.scoreLabel.setSizePolicy(
            QSizePolicy.Policy.Minimum,
            QSizePolicy.Policy.Fixed
        )

        self.horizontalLayout_2.setAlignment(Qt.AlignmentFlag.AlignBottom | Qt.AlignmentFlag.AlignLeft)
        self.horizontalLayout_2.setContentsMargins(5, 5, 5, 5)
        self.horizontalLayout_2.addWidget(self.statusLabel)
        self.horizontalLayout_2.addWidget(self.courseClassCreditLabel)

        self.verticalLayout.setContentsMargins(5, 5, 5, 5)
        self.verticalLayout.addWidget(self.coverLabel)
        self.verticalLayout.addWidget(self.titleLabel, 0)
        self.verticalLayout.addWidget(self.archivedFinalScoreLabel, 0)
        self.horizontalLayout.addWidget(self.progressBar, 0)
        self.horizontalLayout.addWidget(self.scoreLabel, 0)
        self.verticalLayout.addLayout(self.horizontalLayout)

    def mouseReleaseEvent(self, event: QMouseEvent):
        if event.button() == Qt.MouseButton.LeftButton and self.rect().contains(event.pos()):
            self.clacked.emit()

    def setBookData(self, bookData: dict):
        courseCoverImg = bookData['courseCoverImg']
        if courseCoverImg:
            self.coverLabel.setUrl(courseCoverImg)
        else:
            self.coverLabel.setImage(SingleIcon.COVER.path())
        self.titleLabel.setText(bookData['courseName'])
        self.archivedFinalScoreLabel.setText(f"留档总成绩：{bookData['archivedFinalScore']}")
        score = bookData['score']
        self.progressBar.setValue(score)
        self.scoreLabel.setText(f"{score}%")
        self.statusLabel.setText(bookData['learnStatusName'])
        self.courseClassCreditLabel.setText(f"{bookData['courseClassCredit']} 学分")
